package com.bailiban.service.impl;

import com.bailiban.entity.Role;
import com.bailiban.entity.Users;
import com.bailiban.mapper.UsersMapper;
import com.bailiban.service.UsersService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * (Users)表服务实现类
 *
 * @author zhulang
 * @since 2020-11-02 09:02:12
 */
@Service("usersService")
public class UsersServiceImpl implements UsersService {
    @Resource
    private UsersMapper usersMapper;

    @Override
    public Integer save(Integer userId, Integer[] ids) {
        return usersMapper.save(userId, ids);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Users queryById(Integer id) {
        return this.usersMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Users> queryAllByLimit(int offset, int limit) {
        return this.usersMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 根据实体类查询
     *
     * @return 对象列表
     */
    @Override
    public List<Users> queryAll(Users users) {
        return this.usersMapper.queryAll(users);
    }

    /**
     * 查询全部
     *
     * @return 对象列表
     */
    @Override
    public PageInfo<Users> findAll(Integer page, Integer size) {

        PageHelper.startPage(page, size);
        List<Users> all = this.usersMapper.findAll();
        PageInfo<Users> pageInfo = new PageInfo<>(all);
        return pageInfo;
    }

    /**
     * 新增数据
     *
     * @param users 实例对象
     * @return 实例对象
     */
    @Override
    public Users insert(Users users) {
        this.usersMapper.insert(users);
        return users;
    }

    /**
     * 修改数据
     *
     * @param users 实例对象
     * @return 实例对象
     */
    @Override
    public Users update(Users users) {
        this.usersMapper.update(users);
        return this.queryById(users.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.usersMapper.deleteById(id) > 0;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Users users = this.usersMapper.queryByUsername(username);
        User user = new User(users.getUsername(), users.getPassword(), users.getStatus() == 1 ? true : false, true, true, true, getAuthorities(users.getRoles()));
        return user;
    }

    public Collection<? extends GrantedAuthority> getAuthorities(List<Role> roleList) {
        List<GrantedAuthority> authorities = new ArrayList<>();
        roleList.forEach(item -> {
            System.out.println("角色名称：" + item.getRolename());
            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority("ROLE_" + item.getRolename().toUpperCase());
            authorities.add(simpleGrantedAuthority);
        });
        return authorities;
    }
}